﻿// <copyright file="Exporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Export
{
    using System;
    using System.IO;
    using System.Windows.Controls;
    using Microsoft.LightSwitch.Framework.Client;

    public static class Exporter
    {
        private static SaveFileDialog dialog;

        static Exporter()
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
            {
                dialog = new SaveFileDialog();
                dialog.Filter = "Comma Separated Value Files (*.csv, *.txt)|*.csv;*.txt";
            });
        }

        public static void ExportAlignments(VisualCollection<Alignment> collection)
        {
            InternalExport(collection, new AlignmentExporter());
        }

        public static void ExportArmies(VisualCollection<Army> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportBaseActions(VisualCollection<BaseAction> collection)
        {
            InternalExport(collection, new BaseActionExporter());
        }

        public static void ExportBaseLandUnits(VisualCollection<BaseLandUnit> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportBaseProvincesAndNeighbours(VisualCollection<BaseProvince> visualCollection)
        {
            throw new NotImplementedException();
        }

        public static void ExportBaseSeaUnits(VisualCollection<BaseSeaUnit> visualCollection)
        {
            throw new NotImplementedException();
        }

        public static void ExportBloodAbilities(VisualCollection<BloodAbility> collection)
        {
            InternalExport(collection, new BloodAbilityExporter());
        }

        public static void ExportBloodlineDerivations(VisualCollection<BloodlineDerivation> collection)
        {
            InternalExport(collection, new BloodlineDerivationExporter());
        }

        public static void ExportBloodlineStrengths(VisualCollection<BloodlineStrength> collection)
        {
            InternalExport(collection, new BloodlineStrengthExporter());
        }

        public static void ExportCharacters(VisualCollection<Character> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportClasses(VisualCollection<CharacterClass> collection)
        {
            InternalExport(collection, new ClassExporter());
        }

        public static void ExportDeities(VisualCollection<Deity> collection)
        {
            InternalExport(collection, new DeityExporter());
        }

        public static void ExportDomains(VisualCollection<Domain> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportDomainProvincesData(VisualCollection<Domain> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportDomainTurns(VisualCollection<DomainTurn> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportGameInformation(VisualCollection<GameInformation> collection)
        {
            throw new NotImplementedException();
        }

        public static void ExportPlayers(VisualCollection<Player> visualCollection)
        {
            throw new NotImplementedException();
        }

        public static void ExportRaces(VisualCollection<Race> collection)
        {
            InternalExport(collection, new RaceExporter());
        }

        public static void ExportRealmSpells(VisualCollection<RealmSpell> collection)
        {
            InternalExport(collection, new RealmSpellExporter());
        }

        public static void ExportSkills(VisualCollection<Skill> collection)
        {
            InternalExport(collection, new SkillExporter());
        }

        public static void ExportTerrains(VisualCollection<Terrain> collection)
        {
            InternalExport(collection, new TerrainExporter());
        }

        private static void InternalExport<T>(VisualCollection<T> collection, IExporter<T> exporter) where T : class
        {
            if (dialog.ShowDialog() == true)
            {
                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                {
                    using (StreamWriter writer = new StreamWriter(dialog.SafeFileName, false))
                    {
                        exporter.WriteHeaders(writer);
                        exporter.ExportData(writer, collection);
                    }
                });
            }
        }
    }
}
